package com.yunhe.authority.service.authority.impl;

import com.yunhe.authority.domain.authority.Firm;
import com.yunhe.authority.domain.authority.ProjectTree;
import com.yunhe.authority.domain.authority.RealStation;
import com.yunhe.authority.domain.authority.Station;
import com.yunhe.authority.domain.company.Company;
import com.yunhe.authority.domain.company.ProjectCompanyTree;
import com.yunhe.authority.domain.option.Item;
import com.yunhe.authority.repository.option.ItemRepository;
import com.yunhe.authority.service.authority.FirmService;
import com.yunhe.authority.service.authority.ProjectTreeService;
import com.yunhe.authority.service.authority.RealStationService;
import com.yunhe.authority.service.authority.StationService;
import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ProjectTreeServiceImpl
 * @Description TODO
 * @Author HeTao
 * @Date 2021/2/24 17:53
 * @Version 1.0
 **/
@Service
public class ProjectTreeServiceImpl implements ProjectTreeService {

    @Autowired
    private FirmService firmService;

    @Autowired
    private StationService stationService;

    @Autowired
    private ItemRepository itemRepository;

    @Autowired
    private RealStationService realStationService;

    @Override
    public List<ProjectTree> getAllProject() {
        List<ProjectTree> projectTrees = new ArrayList<>();
        //查询所有单位
        Iterable<Firm> firms = firmService.findAllFirms();
        if(firms != null){
            for (Firm firm : firms) {
                //通过单位id 查询电站信息
                ProjectTree firmTree = buildTree(firm.getId(), firm.getEnterpriseFullName(), firm.getName(),null, 1, "firm");
                Iterable<Station> stations = stationService.findByFirmId(firm.getId());
                if(stations != null){
                    for (Station station : stations) {
                        ProjectTree stationTree = buildTree(station.getId(), station.getEnterpriseFullName(),station.getName(), firm.getId(), 2,  "station");
                        firmTree.addChild(stationTree);
                    }
                }
                projectTrees.add(firmTree);
            }
        }
        return projectTrees;
    }

    @Override
    public List<ProjectTree> getTree() {
        List<ProjectTree> projectTrees = new ArrayList<>();
        //获取系统平台的枚举条目
        Iterable<Item> items = itemRepository.findAllItems("system");
        if(items != null){
            for (Item item : items) {
                ProjectTree systemTree = buildTree(item.getId(), item.getTitle(),item.getCode(), null, 1, "system");
                //通过系统查询单位
                Iterable<Firm> firms = firmService.findBySystem(item.getCode());
                if(firms != null){
                    for (Firm firm : firms) {
                        ProjectTree firmTree = buildTree(firm.getId(), firm.getEnterpriseFullName(), firm.getName(), item.getId(), 2, "firm");
                        Iterable<Station> stations = stationService.findByFirmId(firm.getId());
                        if(stations != null){
                            for (Station station : stations) {
                                ProjectTree stationTree = buildTree(station.getId(), station.getEnterpriseFullName(),station.getName(), firm.getId(), 3,  "station");
                                Iterable<RealStation> realStations = realStationService.findRealStationByStationId(station.getId());
                                if(realStations != null){
                                    for (RealStation realStation : realStations) {
                                        ProjectTree realStationTree = buildTree(realStation.getId(), StringUtil.isEmpty(realStation.getAbbreviationName())?
                                                realStation.getEnterpriseFullName():realStation.getAbbreviationName(), realStation.getName(), station.getId(), 4, "realStation");
                                        stationTree.addChild(realStationTree);
                                    }
                                }
                                firmTree.addChild(stationTree);
                            }
                        }
                        systemTree.addChild(firmTree);
                    }
                }
                projectTrees.add(systemTree);
            }
        }
        return projectTrees;
    }

    @Override
    public List<ProjectTree> getRealStationTree() {
        List<ProjectTree> projectTrees = new ArrayList<>();
        //获取项目id
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            return null;
        }
        Long stationId = Long.valueOf(projectCode);
        Firm firm = firmService.findByStationId(stationId);
        if(firm !=null){
            ProjectTree firmTree = buildTree(firm.getId(), firm.getEnterpriseFullName(), firm.getName(), null, 1, "firm");
            Station station = stationService.findById(stationId);
            if(station != null){
                ProjectTree stationTree = stationTree = buildTree(station.getId(), station.getEnterpriseFullName(),station.getName(), firm.getId(), 2,  "station");
                Iterable<RealStation> realStations = realStationService.findRealStationByStationId(stationId);
                if(realStations != null){
                    for (RealStation realStation : realStations) {
                        ProjectTree realStationTree = buildTree(realStation.getId(), StringUtil.isEmpty(realStation.getAbbreviationName()) ?
                                realStation.getEnterpriseFullName() : realStation.getAbbreviationName(), realStation.getName(), station.getId(), 3, "realStation");
                        stationTree.addChild(realStationTree);
                    }
                }
                firmTree.addChild(stationTree);
            }
            projectTrees.add(firmTree);
        }
        return projectTrees;
    }

    @Override
    public List<ProjectTree> getStationUntilRealStationTree() {
        List<ProjectTree> projectTrees = new ArrayList<>();
        //获取项目id
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            return null;
        }
        Long stationId = Long.valueOf(projectCode);
        Station station = stationService.findById(stationId);
        if(station != null){
            ProjectTree stationTree = buildTree(station.getId(), station.getEnterpriseFullName(),station.getName(), null, 1,  "station");
            Iterable<RealStation> realStations = realStationService.findRealStationByStationId(stationId);
            if(realStations != null){
                for (RealStation realStation : realStations) {
                    ProjectTree realStationTree = buildTree(realStation.getId(), StringUtil.isEmpty(realStation.getAbbreviationName()) ?
                            realStation.getEnterpriseFullName() : realStation.getAbbreviationName(), realStation.getName(), station.getId(), 2, "realStation");
                    stationTree.addChild(realStationTree);
                }
            }
            projectTrees.add(stationTree);
        }
        return projectTrees;
    }

    private ProjectTree buildTree(Long id, String title,String code, Long parentId, int level,  String type) {
        ProjectTree projectTree = new ProjectTree();
        projectTree.setTitle(title);
        projectTree.setId(id);
        projectTree.setCode(code);
        projectTree.setParentId(parentId);
        projectTree.setLevel(level);
        projectTree.setType(type);
        return projectTree;
    }
}
